package com.wdhcr.repository;

import com.wdhcr.entity.Blog;
import com.wdhcr.entity.Tag;
import com.wdhcr.entity.Type;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

public interface BlogRepository  extends JpaRepository<Blog,Long>{

    @Transactional //更新操作需要事务设置
    @Modifying
    @Query("update Blog b set b.title=?2,b.content=?3,b.html=?4,b.firstImageUrl=?5,b.flag=?6," +
            "b.published=?7,b.updateTime=?8,b.description=?9,b.tag=?10,b.type=?11 where b.id =?1")
    int updateBlog(Long id,String title, String content, String html, String firstImageUrl,
                    String flag, Boolean published, Date updateTime,
                    String description, Tag tag, Type type);

    @Query("select b from Blog b where b.title like ?1 or b.content like ?1")
    Page<Blog> findByQuery(String query, Pageable pageable);

    @Query("select b from Blog b where b.published = true")
    Page<Blog> findPubAll(Pageable pageable);

    Page<Blog> findBlogByTypeAndPublishedTrue(Type type,Pageable pageable);

    Page<Blog> findBlogByTagAndPublishedTrue(Tag tag,Pageable pageable);

    @Query("select b from Blog b where b.published = true and (b.title like ?1 or b.content like ?1)")
    List<Blog> findByQueryPub(String query, Sort sort);



}
